home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_10_08
/
ramey.exe
/
FIO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-10-14
|
2KB
|
80 lines
/*
Copyright (c) Robert Ramey 1991. All Rights Reserved
*/
#include <stdio.h>
#include <stdlib.h>
#include "psort.h"
#include "io.h"
private
MEM_SIZE buffer_size = B_SIZE;
/* size of sequencial i/o buffers in K*/
private
char *io_buffer;
/* use same buffer for input and output to save space */
/*********************************************************************
io_init - setup io
**********************************************************************/
void
io_init(argc, argv)
int argc;
char *argv[];
{
int i;
i = arg_find(argc, argv, "-b");
if( i == -1)
i = arg_find(argc, argv, "-B");
if(i != -1){
argv[i] = "";
arg_value(argv[++i], &buffer_size);
if(buffer_size > MAX_BUFFER_SIZE)
buffer_size = MAX_BUFFER_SIZE;
argv[i] = "";
}
io_buffer = malloc(buffer_size * K);
if(!io_buffer)
error("Couldn't get space for i/o buffers");
if(setvbuf(stdin, io_buffer, _IOFBF, buffer_size * K)
|| setvbuf(stdout, io_buffer, _IOFBF, buffer_size * K))
error("Couldn't set i/o buffers");
return;
}
/*********************************************************************
efwrite - write a record to disk and check for error
**********************************************************************/
void
efwrite(source, size, count, fptr)
char *source;
unsigned int size, count;
FILE *fptr;
{
int i;
i = fwrite(source, size, count, fptr);
if(i != count){
perror("Error on file write");
exit(1);
}
}
/*********************************************************************
efread - read a record from disk and check for error
**********************************************************************/
int
efread(dest, size, count, fptr)
char *dest;
unsigned int size, count;
FILE *fptr;
{
int i;
i = fread(dest, size, count, fptr);
if(i != count){
perror("Error on file read");
exit(1);
}
return count;
}